import Axiox from 'axios'
Axiox.defaults.baseURL = 'http://39.98.208.174:8080/'
Axiox.defaults.headers.common['authorization'] = sessionStorage.getItem('token')
function axios(type, url, data = {}) {
  if (Axiox.defaults.headers.common['authorization'] == null) {
    Axiox.defaults.headers.common['authorization'] = sessionStorage.getItem('token')
  }
  return new Promise((resolve, reject) => {
    let params = type == 'get' ? { 'params': data } : data
    Axiox[type](url, params
    ).then((res) => {
      // console.log(res);
      if (res.data.status == 200) {
        resolve(res.data.data)
      } else {
        if (res.data.msg) {
          alert(res.data.msg)
          if (res.data.msg == "token无效或已过期") {
            this.$router.push('/login')
          }
          return
        }
        let list = Object.keys(res.data)
        if (list.indexOf('message')) {
          alert(res.data.message)
        } else {
          alert(res.data.data)
        }
        // reject(res.data)
      }
    })
  })
}


export default axios
